package org.zfes.snowier.zbss.xzy.biz.device.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZObjectUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.data.ParamMap;
import org.zfes.snowier.zbss.xzy.biz.device.dao.DeviceModelMapper;
import org.zfes.snowier.zbss.xzy.biz.device.model.DeviceModel;
import org.zfes.snowier.zbss.xzy.biz.device.service.IDeviceModelService;

import com.google.common.collect.Lists;

import org.zfes.snowier.zbss.xzy.biz.device.service.IDeviceModelService;

import java.util.stream.Stream;
@Service
public class DeviceModelServiceImpl implements IDeviceModelService{
 	@Autowired
	private DeviceModelMapper deviceModelMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createDeviceModel(DeviceModel deviceModel) {
		ZBeanUtil.setCreateInfo(deviceModel);
		ZBeanUtil.validateBean(deviceModel);
		deviceModelMapper.insertSelective(deviceModel);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<DeviceModel> loadDeviceModelById(Long id) {
		if(id!=null){
			return Optional.ofNullable(deviceModelMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateDeviceModel(DeviceModel deviceModel) {
		ZBeanUtil.setUpdatedInfo(deviceModel);
		ZBeanUtil.validateBean(deviceModel);
		deviceModelMapper.updateByPrimaryKeySelective(deviceModel);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteDeviceModel(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		deviceModelMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setDeviceModelStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		deviceModelMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadDeviceModelDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(deviceModelMapper.selectListCount(pm), deviceModelMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setStatusByTypeId(Long typeId, Byte status) {
		if(typeId==null) {
			return;
		}
		deviceModelMapper.updateStatusByTypeId(typeId, status);
		
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<DeviceModel> loadByDeviceType(Long typeId, Byte status) {
		if(typeId==null) {
			return	Lists.newArrayList();
		}
		return deviceModelMapper.selectByTypeId(typeId, status);
	}
	
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<DeviceModel> loadByDeviceTypeList(List<Long> typeIdList, Byte status){
		if(ZObjectUtil.isEmpty(typeIdList)) {
			return Lists.newArrayList();
		}
		return deviceModelMapper.selectByTypeList( typeIdList,  status);
	}
	
}